package com.klm.easymq.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Easy-MQ 配置属性类
 * 所有配置都有默认值，确保SDK在没有外部配置文件时也能正常工作
 */
@Data
@ConfigurationProperties(prefix = "easy-mq")
public class EasyMqProperties {
    
    /**
     * 是否启用Easy-MQ
     */
    private boolean enabled = true;
    
    /**
     * 是否自动配置
     */
    private boolean autoConfig = true;
    
    /**
     * 生产者配置
     */
    private Producer producer = new Producer();
    
    /**
     * 消费者配置
     */
    private Consumer consumer = new Consumer();
    
    /**
     * 幂等控制配置
     */
    private Idempotent idempotent = new Idempotent();
    
    /**
     * 监控配置
     */
    private Monitor monitor = new Monitor();
    
    /**
     * 序列化配置
     */
    private Serialization serialization = new Serialization();
    
    /**
     * 异常处理配置
     */
    private Exception exception = new Exception();
    
    /**
     * 日志配置
     */
    private Logging logging = new Logging();
    
    /**
     * 健康检查配置
     */
    private Health health = new Health();
    
    /**
     * 指标配置
     */
    private Metrics metrics = new Metrics();
    
    /**
     * Web控制台配置
     */
    private Console console = new Console();
    
    @Data
    public static class Producer {
        private int sendTimeout = 3000;
        private int retryTimes = 2;
        private int asyncRetryTimes = 2;
        private int compressThreshold = 4096;
        private int maxMessageSize = 4194304;
    }
    
    @Data
    public static class Consumer {
        private int pullBatchSize = 10;
        private int consumeTimeout = 15;
        private int concurrency = 1;
        private int maxConcurrency = 20;
    }
    
    @Data
    public static class Idempotent {
        /**
         * 是否启用幂等控制
         */
        private boolean enabled = true;
        
        /**
         * 存储类型：caffeine, redis
         */
        private String storage = "caffeine";
        
        /**
         * Caffeine配置
         */
        private Caffeine caffeine = new Caffeine();
        
        /**
         * Redis配置
         */
        private Redis redis = new Redis();
    }
    
    @Data
    public static class Caffeine {
        /**
         * 最大缓存条目数
         */
        private long maximumSize = 10000;
        
        /**
         * 写入后过期时间（秒）
         */
        private long expireAfterWrite = 3600;
        
        /**
         * 访问后过期时间（秒）
         */
        private long expireAfterAccess = 1800;
    }
    
    @Data
    public static class Redis {
        /**
         * 键前缀
         */
        private String keyPrefix = "easymq:idempotent:";
        
        /**
         * 过期时间（秒）
         */
        private long expireSeconds = 3600;
        
        /**
         * Redis数据库索引
         */
        private int database = 0;
    }
    
    @Data
    public static class Monitor {
        /**
         * 是否启用监控
         */
        private boolean enabled = true;
        
        /**
         * 存储类型：sqlite, memory, enhanced-sqlite
         */
        private String storage = "sqlite";
        
        /**
         * SQLite配置
         */
        private Sqlite sqlite = new Sqlite();
    }
    
    @Data
    public static class Sqlite {
        /**
         * 数据库文件路径
         */
        private String dbPath = "easymq-monitor.db";
        
        /**
         * 最大记录数
         */
        private int maxRecords = 100000;
        
        /**
         * 保留天数
         */
        private int retentionDays = 30;
        
        /**
         * 是否启用WAL模式
         */
        private boolean walMode = true;
        
        /**
         * 连接池大小
         */
        private int poolSize = 10;
    }
    
    @Data
    public static class Serialization {
        /**
         * 序列化类型：json, protobuf
         */
        private String type = "json";
        
        /**
         * JSON配置
         */
        private Json json = new Json();
        
        /**
         * Protobuf配置
         */
        private Protobuf protobuf = new Protobuf();
    }
    
    @Data
    public static class Json {
        /**
         * 日期格式
         */
        private String dateFormat = "yyyy-MM-dd HH:mm:ss";
        
        /**
         * 时区
         */
        private String timeZone = "GMT+8";
        
        /**
         * 是否包含null值
         */
        private boolean includeNull = false;
    }
    
    @Data
    public static class Protobuf {
        /**
         * 是否启用压缩
         */
        private boolean compression = false;
    }
    
    @Data
    public static class Exception {
        private boolean retryEnabled = true;
        private int maxRetryTimes = 3;
        private int retryInterval = 1000;
        private boolean logEnabled = true;
    }
    
    @Data
    public static class Logging {
        private boolean verbose = false;
        private String level = "info";
        private boolean logMessageContent = false;
        private int maxMessageLength = 1000;
    }
    
    @Data
    public static class Health {
        /**
         * 是否启用健康检查
         */
        private boolean enabled = true;
        
        /**
         * 检查间隔（毫秒）
         */
        private long checkInterval = 30000;
        
        /**
         * 超时时间（毫秒）
         */
        private long timeout = 5000;
        
        /**
         * 是否显示详细信息
         */
        private boolean detailed = true;
    }
    
    @Data
    public static class Metrics {
        /**
         * 是否启用指标收集
         */
        private boolean enabled = true;
        
        /**
         * 是否显示详细信息
         */
        private boolean detailed = true;
        
        /**
         * 保留时间（分钟）
         */
        private int retentionMinutes = 60;
        
        /**
         * Prometheus配置
         */
        private Prometheus prometheus = new Prometheus();
    }
    
    @Data
    public static class Prometheus {
        /**
         * 是否启用Prometheus
         */
        private boolean enabled = true;
        
        /**
         * 指标路径
         */
        private String path = "/actuator/prometheus";
    }
    
    @Data
    public static class Console {
        /**
         * 是否启用Web控制台
         */
        private boolean enabled = true;
        
        /**
         * 控制台端口（0表示使用主应用端口）
         */
        private int port = 8080;
        
        /**
         * 上下文路径
         */
        private String contextPath = "/easymq";
        
        /**
         * 是否启用安全认证
         */
        private boolean securityEnabled = false;
        
        /**
         * 用户名
         */
        private String username = "admin";
        
        /**
         * 密码
         */
        private String password = "admin123";
        
        /**
         * 允许访问的IP地址（逗号分隔）
         */
        private String allowedIps = "";
        
        /**
         * 是否启用CORS
         */
        private boolean corsEnabled = true;
        
        /**
         * 控制台主题
         */
        private String theme = "default";
        
        /**
         * 是否启用实时数据更新
         */
        private boolean realtimeEnabled = true;
        
        /**
         * 数据刷新间隔（秒）
         */
        private int refreshInterval = 5;
    }
} 